package com.mes.biz.service.impl;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.*;
import com.mes.biz.mapper.BizProcessPlanMapper;
import com.mes.biz.service.BizProcessPlanService;
import com.mes.biz.service.BizProductProcessDetailService;
import com.mes.biz.service.BizProductionProcessService;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.utils.SecurityUtils;
import com.mes.system.strategy.AutoCodeUtil;
import org.apache.commons.compress.utils.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 工序计划Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-05-03
 */
@Service
@Transactional(readOnly = true)
public class BizProcessPlanServiceImpl extends BaseServiceImpl<BizProcessPlanMapper, BizProcessPlan> implements BizProcessPlanService {

    private static final Logger log = LoggerFactory.getLogger(BizProcessPlanServiceImpl.class);
    @Resource
    private AutoCodeUtil autoCodeUtil;
    @Resource
    private BizProductProcessDetailService bizProductProcessDetailService;
    /**
     * 获取单条数据
     * @param bizProcessPlan 工序计划
     * @return 工序计划
     */
    @Override
    public BizProcessPlan get(BizProcessPlan bizProcessPlan) {
        BizProcessPlan dto = super.get(bizProcessPlan);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 工序计划id
     * @return 工序计划
     */
    @Override
    public BizProcessPlan get(String id) {
        BizProcessPlan dto = super.get(id);
        List<BizProductProcessDetail> bizProductProcessDetailList = bizProductProcessDetailService.getBizProductProcessDetailByProcessPlanId(id);
        dto.setProcessList(bizProductProcessDetailList);
        return dto;
    }

    /**
     * 查询工序计划列表
     * @param bizProcessPlan 工序计划
     * @return 工序计划
     */
    @Override
    public List<BizProcessPlan> findList(BizProcessPlan bizProcessPlan) {
		List<BizProcessPlan> bizProcessPlanList = super.findList(bizProcessPlan);
        return bizProcessPlanList;
    }

    /**
     * 分页查询工序计划列表
     * @param bizProcessPlan 工序计划
     * @return 工序计划
     */
    @Override
    public PageInfo<BizProcessPlan> findPage(BizProcessPlan bizProcessPlan) {
		PageInfo<BizProcessPlan> page = super.findPage(bizProcessPlan);
        return page;
    }

    /**
     * 保存工序计划
     * @param bizProcessPlan
     * @return 结果
     */
    @Override
    public boolean save(BizProcessPlan bizProcessPlan) {
        return super.save(bizProcessPlan);
    }

    /**
     * 删除工序计划信息
     * @param bizProcessPlan
     * @return 结果
     */
    @Override
    public boolean remove(BizProcessPlan bizProcessPlan) {
        return super.remove(bizProcessPlan);
    }

    /**
     * 批量删除工序计划
     * @param ids 需要删除的工序计划ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBizProcessPlanByIds(String[] ids) {
        return mapper.deleteBizProcessPlanByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Override
    @Transactional
    public boolean savePlan(BizProcessPlan bizProcessPlan) {
        bizProcessPlan.setTenantId(SecurityUtils.getTenantId());
        String code = autoCodeUtil.genSerialCode("PROCESS_PLAN_CODE",null);
        bizProcessPlan.setProcessNumber(code);
        this.save(bizProcessPlan);

        //保存计划明细
        List<BizProductProcessDetail> bizProductProcessDetailList = bizProcessPlan.getProcessList();
        for(BizProductProcessDetail bizProductProcessDetail:bizProductProcessDetailList) {
            bizProductProcessDetail.setProductionProcessId(bizProductProcessDetail.getProductionProcessId());
            bizProductProcessDetail.setId(null);
            bizProductProcessDetail.preInsert();
            bizProductProcessDetail.setProductProcessId(bizProcessPlan.getId());
            bizProductProcessDetail.setTenantId(SecurityUtils.getTenantId());
        }
        bizProductProcessDetailService.saveBatch(bizProductProcessDetailList);

        return true;
    }

    @Override
    @Transactional
    public boolean updatePlan(BizProcessPlan bizProcessPlan) {
        this.save(bizProcessPlan);

        //保存计划明细
        List<BizProductProcessDetail> bizProductProcessDetailList = bizProcessPlan.getProcessList();

        if (bizProductProcessDetailList !=null && bizProductProcessDetailList.size()>0){
            Set<String> existsSets = Sets.newHashSet();
            List<BizProductProcessDetail> oldList = bizProductProcessDetailService.getBizProductProcessDetailByProcessPlanId(bizProcessPlan.getId());
            if (oldList !=null && oldList.size()>0){
                oldList.forEach(existsVO -> {
                    existsSets.add(existsVO.getId());
                });
            }
            for(BizProductProcessDetail bizProductProcessDetail:bizProductProcessDetailList) {
                bizProductProcessDetail.setProductionProcessId(bizProductProcessDetail.getProductionProcessId());
                bizProductProcessDetail.setProductProcessId(bizProcessPlan.getId());
                bizProductProcessDetail.setTenantId(SecurityUtils.getTenantId());
                existsSets.remove(bizProductProcessDetail.getId());
                bizProductProcessDetailService.save(bizProductProcessDetail);
            }
            if (existsSets.size()>0){
                String[] ids = existsSets.toArray(new String[0]);
                bizProductProcessDetailService.deleteBizProductProcessDetailByIds(ids);
            }
        }
        return true;
    }
}
